from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.template import loader

from blogs.models import Blog
from user.models import User, Friend


def login(request):
    dic = {}

    request.COOKIES.clear()
    if request.method == 'GET':
        return render(request,'user/login.html',dic)
    elif request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        try:
            user = User.objects.get(username=username,password=password)
        except:
            dic['message'] = '账号或密码错误'
            return render(request,'user/login.html',dic)

        blogs = Blog.objects.all().order_by('-id')
        dic['blogs'] = blogs
        t = loader.get_template('index.html')
        html = t.render(dic)
        response = HttpResponse(html)
        response.set_cookie('user_id', str(user.id))
        return response





def register(request):
    dic = {}
    if request.method == 'GET':
        return render(request,'user/register.html')
    elif request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        email = request.POST.get('email')
        birthday = request.POST.get('birthday')
        print(username, password,email,birthday)
        try:
            User.objects.create(username=username,password=password,email=email,birthday=birthday)
        except:
            dic['register'] = '账号存在,请重试'
            return render(request, 'user/register.html', dic)

        dic['register'] = '注册成功,请登录'
        return render(request, 'user/login.html', dic)


def myself_info(request):
    dic = {}
    if request.method == "GET":
        return render(request,'user/myself_info.html')
    elif request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        password_again = request.POST.get('password_again')
        email = request.POST.get('email')
        birthday = request.POST.get('birthday')

        print(username,password,password_again,email,birthday)
        if password == password_again:
            User.objects.filter(username=username).update(username=username,password=password,email=email,birthday=birthday)
            dic['mes'] = '修改成功'
            return render(request,'user/myself_info.html',dic)
        else:
            dic['mes'] = '密码不一致'
            return render(request,'user/myself_info.html',dic)


def exit(request):
    request.COOKIES.clear()
    return redirect('/user/login')


def addfriend(request,num,n):

    dic = {}
    print(num)
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)
    try:
        user1 = User.objects.get(id=user_id,other_id=num)
    except:
        Friend.objects.create(other_id=num,user_id=user.id)
    dic['friend'] = '关注'
    return redirect(f'/blogs/{n}')


def del_friend(request,num):
    user_id = request.COOKIES.get('user_id')
    user = User.objects.get(id=user_id)

    Friend.objects.get(user_id=user.id,other_id=num).delete()
    return redirect('/blogs/friend')